<?php

/**
 * PageController.php
 *
 * Holds abstract class PageController
 *
 * @package Application
 * @subpackage Controllers
 */

/**
 * PageController
 *
 * Public page base controller
 *
 * @package Application
 * @subpackage Controllers
 */
abstract class PageController extends Controller {

	/**
	 * Logged in user, if available
	 *
	 * @var User
	 */
	protected $user;

	/**
	 * @var Mvc_Request
	 */
	protected $request;

	/**
	 * Initializes page
	 *
	 * @return PageController (fluent interface)
	 */
	public function init() {
		parent::init();

		//Initialise the session singleton
		$session = Session::instance();

		//if a user is set, make sure it is available in the templates
		if(isset($session->gebruiker_id)){
			$mG = new Mapper_Gebruiker($this->getDb());
			$this->set("gebruiker", $this->gebruiker = $mG->get($session->gebruiker_id));
		}

		//get the standard language
		$language = $this->getConfig()->get('language');

		//get the language file itself and import it
		$this->getLanguage()->setLanguagePath(APP . 'lang/' . $language . '/');
		$this->getLanguage()->/*import('common.php')->*/import('error.php');

		//create the language file object('s)
		$this->i18n = new Mvc_LanguageObject($this->getLanguage()->export());

		//add some more relevant information for the template pages.
		$this->language = $language;
		$this->module = strtolower($this->_module);
		$this->controller = strtolower(substr($this->_controller,0,strlen($this->_controller) - strlen("Controller")));
		$this->action = strtolower(substr($this->_action,6));

		//saves on calls, speeds up large amount of request-accesses
		$this->request = $this->getRequest();

		//are we developing or live? DEVELOPMENT is set to true or false in the index.php in the root directory
		$this->development = DEVELOPMENT;

		return $this;
	}


	/**
	 * Return boolean on if this request is a JSON request.
	 * If it is a JSON request, call setPureJSON and return true, else return false
	 *
	 * @param boolean $post
	 */
	protected function isJSON($post = true){
		if(	$post && $this->request->isPost()
		||	!$post && $this->request->isGet()){
			$this->setPureJSON();
			return true;
		}

		return false;
	}
}
